home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / perlman / man / perlrun.txt < prev    next >
Encoding:
Text File  |  1999-09-09  |  18.3 KB  |  433 lines

  1. NAME
  2.        perlrun - how to execute the Perl interpreter
  3.  
  4. SYNOPSIS
  5.        perl [ -sTuU ]      [ -hv ] [ -V[:configvar] ]
  6.             [ -cw ] [ -d[:debugger] ] [ -D[number/list] ]
  7.             [ -pna ] [ -Fpattern ] [ -l[octal] ] [ -0[octal] ]
  8.             [ -Idir ] [ -m[-]module ] [ -M[-]'module...' ]
  9.             [ -P ]      [ -S ]      [ -x[dir] ]
  10.             [ -i[extension] ]
  11.             [ -e 'command' ] [ -- ] [ programfile ] [ argument ]...
  12.  
  13. DESCRIPTION
  14.        Upon startup, Perl looks for your script in one of the
  15.        following places:
  16.  
  17.        1.  Specified line by line via -e switches on the command
  18.            line.
  19.  
  20.        2.  Contained in the file specified by the first filename
  21.            on the command line.  (Note that systems supporting
  22.            the #! notation invoke interpreters this way.)
  23.  
  24.        3.  Passed in implicitly via standard input.  This only
  25.            works if there are no filename arguments--to pass
  26.            arguments to a STDIN script you must explicitly
  27.            specify a "-" for the script name.
  28.  
  29.        With methods 2 and 3, Perl starts parsing the input file
  30.        from the beginning, unless you've specified a -x switch,
  31.        in which case it scans for the first line starting with #!
  32.        and containing the word "perl", and starts there instead.
  33.        This is useful for running a script embedded in a larger
  34.        message.  (In this case you would indicate the end of the
  35.        script using the __END__ token.)
  36.  
  37.        As of Perl 5, the #! line is always examined for switches
  38.        as the line is being parsed.  Thus, if you're on a machine
  39.        that only allows one argument with the #! line, or worse,
  40.        doesn't even recognize the #! line, you still can get
  41.        consistent switch behavior regardless of how Perl was
  42.        invoked, even if -x was used to find the beginning of the
  43.        script.
  44.  
  45.        Because many operating systems silently chop off kernel
  46.        interpretation of the #! line after 32 characters, some
  47.        switches may be passed in on the command line, and some
  48.        may not; you could even get a "-" without its letter, if
  49.        you're not careful.  You probably want to make sure that
  50.        all your switches fall either before or after that 32
  51.        character boundary.  Most switches don't actually care if
  52.        they're processed redundantly, but getting a - instead of
  53.        a complete switch could cause Perl to try to execute
  54.        standard input instead of your script.  And a partial -I
  55.        switch could also cause odd results.
  56.  
  57.        Parsing of the #! switches starts wherever "perl" is
  58.        mentioned in the line.  The sequences "-*" and "- " are
  59.        specifically ignored so that you could, if you were so
  60.        inclined, say
  61.  
  62.            #!/bin/sh -- # -*- perl -*- -p
  63.            eval 'exec perl $0 -S ${1+"$@"}'
  64.                if 0;
  65.  
  66.        to let Perl see the -p switch.
  67.  
  68.        If the #! line does not contain the word "perl", the
  69.        program named after the #! is executed instead of the Perl
  70.        interpreter.  This is slightly bizarre, but it helps
  71.        people on machines that don't do #!, because they can tell
  72.        a program that their SHELL is /usr/bin/perl, and Perl will
  73.        then dispatch the program to the correct interpreter for
  74.        them.
  75.  
  76.        After locating your script, Perl compiles the entire
  77.        script to an internal form.  If there are any compilation
  78.        errors, execution of the script is not attempted.  (This
  79.        is unlike the typical shell script, which might run
  80.        partway through before finding a syntax error.)
  81.  
  82.        If the script is syntactically correct, it is executed.
  83.        If the script runs off the end without hitting an exit()
  84.        or die() operator, an implicit exit(0) is provided to
  85.        indicate successful completion.
  86.  
  87.        Switches
  88.  
  89.        A single-character switch may be combined with the
  90.        following switch, if any.
  91.  
  92.            #!/usr/bin/perl -spi.bak    # same as -s -p -i.bak
  93.  
  94.        Switches include:
  95.  
  96.        -0[digits]
  97.             specifies the record separator ($/) as an octal
  98.             number.  If there are no digits, the null character
  99.             is the separator.  Other switches may precede or
  100.             follow the digits.  For example, if you have a
  101.             version of find which can print filenames terminated
  102.             by the null character, you can say this:
  103.  
  104.                 find . -name '*.bak' -print0 | perl -n0e unlink
  105.  
  106.             The special value 00 will cause Perl to slurp files
  107.             in paragraph mode.  The value 0777 will cause Perl to
  108.             slurp files whole since there is no legal character
  109.             with that value.
  110.  
  111.        -a   turns on autosplit mode when used with a -n or -p.
  112.             An implicit split command to the @F array is done as
  113.             the first thing inside the implicit while loop
  114.             produced by the -n or -p.
  115.  
  116.                 perl -ane 'print pop(@F), "\n";'
  117.  
  118.             is equivalent to
  119.  
  120.                 while (<>) {
  121.                     @F = split(' ');
  122.                     print pop(@F), "\n";
  123.                 }
  124.  
  125.             An alternate delimiter may be specified using -F.
  126.  
  127.        -c   causes Perl to check the syntax of the script and
  128.             then exit without executing it.  Actually, it will
  129.             execute BEGIN, END, and use blocks, since these are
  130.             considered as occurring outside the execution of your
  131.             program.
  132.  
  133.        -d   runs the script under the Perl debugger.  See the
  134.             perldebug manpage.
  135.  
  136.        -d:foo
  137.             runs the script under the control of a debugging or
  138.             tracing module installed as Devel::foo. E.g.,
  139.             -d:DProf executes the script using the Devel::DProf
  140.             profiler.  See the perldebug manpage.
  141.  
  142.        -Dnumber
  143.  
  144.        -Dlist
  145.             sets debugging flags.  To watch how it executes your
  146.             script, use -D14.  (This only works if debugging is
  147.             compiled into your Perl.)  Another nice value is
  148.             -D1024, which lists your compiled syntax tree.  And
  149.             -D512 displays compiled regular expressions. As an
  150.             alternative specify a list of letters instead of
  151.             numbers (e.g. -D14 is equivalent to -Dtls):
  152.  
  153.                     1  p  Tokenizing and Parsing
  154.                     2  s  Stack Snapshots
  155.                     4  l  Label Stack Processing
  156.                     8  t  Trace Execution
  157.                    16  o  Operator Node Construction
  158.                    32  c  String/Numeric Conversions
  159.                    64  P  Print Preprocessor Command for -P
  160.                   128  m  Memory Allocation
  161.                   256  f  Format Processing
  162.                   512  r  Regular Expression Parsing
  163.                  1024  x  Syntax Tree Dump
  164.                  2048  u  Tainting Checks
  165.                  4096  L  Memory Leaks (not supported anymore)
  166.                  8192  H  Hash Dump -- usurps values()
  167.                 16384  X  Scratchpad Allocation
  168.                 32768  D  Cleaning Up
  169.  
  170.        -e commandline
  171.             may be used to enter one line of script.  If -e is
  172.             given, Perl will not look for a script filename in
  173.             the argument list.  Multiple -e commands may be given
  174.             to build up a multi-line script.  Make sure to use
  175.             semicolons where you would in a normal program.
  176.  
  177.        -Fpattern
  178.             specifies the pattern to split on if -a is also in
  179.             effect.  The pattern may be surrounded by //, "" or
  180.             '', otherwise it will be put in single quotes.
  181.  
  182.        -h   prints a summary of the options.
  183.  
  184.        -i[extension]
  185.             specifies that files processed by the <> construct
  186.             are to be edited in-place.  It does this by renaming
  187.             the input file, opening the output file by the
  188.             original name, and selecting that output file as the
  189.             default for print() statements.  The extension, if
  190.             supplied, is added to the name of the old file to
  191.             make a backup copy.  If no extension is supplied, no
  192.             backup is made.  From the shell, saying
  193.  
  194.                 $ perl -p -i.bak -e "s/foo/bar/; ... "
  195.  
  196.             is the same as using the script:
  197.  
  198.                 #!/usr/bin/perl -pi.bak
  199.                 s/foo/bar/;
  200.  
  201.             which is equivalent to
  202.  
  203.                 #!/usr/bin/perl
  204.                 while (<>) {
  205.                     if ($ARGV ne $oldargv) {
  206.                         rename($ARGV, $ARGV . '.bak');
  207.                         open(ARGVOUT, ">$ARGV");
  208.                         select(ARGVOUT);
  209.                         $oldargv = $ARGV;
  210.                     }
  211.                     s/foo/bar/;
  212.                 }
  213.                 continue {
  214.                     print;  # this prints to original filename
  215.                 }
  216.                 select(STDOUT);
  217.  
  218.             except that the -i form doesn't need to compare $ARGV
  219.             to $oldargv to know when the filename has changed.
  220.             It does, however, use ARGVOUT for the selected
  221.             filehandle.  Note that STDOUT is restored as the
  222.             default output filehandle after the loop.
  223.  
  224.             You can use eof without parenthesis to locate the end
  225.             of each input file, in case you want to append to
  226.             each file, or reset line numbering (see example in
  227.             the eof entry in the perlfunc manpage).
  228.  
  229.        -Idirectory
  230.             Directories specified by -I are prepended to the
  231.             search path for modules (@INC), and also tells the C
  232.             preprocessor where to search for include files.  The
  233.             C preprocessor is invoked with -P; by default it
  234.             searches /usr/include and /usr/lib/perl.
  235.  
  236.        -l[octnum]
  237.             enables automatic line-ending processing.  It has two
  238.             effects:  first, it automatically chomps the line
  239.             terminator when used with -n or -p, and second, it
  240.             assigns "$\" to have the value of octnum so that any
  241.             print statements will have that line terminator added
  242.             back on.  If octnum is omitted, sets "$\" to the
  243.             current value of "$/".  For instance, to trim lines
  244.             to 80 columns:
  245.  
  246.                 perl -lpe 'substr($_, 80) = ""'
  247.  
  248.             Note that the assignment $\ = $/ is done when the
  249.             switch is processed, so the input record separator
  250.             can be different than the output record separator if
  251.             the -l switch is followed by a -0 switch:
  252.  
  253.                 gnufind / -print0 | perl -ln0e 'print "found $_" if -p'
  254.  
  255.             This sets $\ to newline and then sets $/ to the null
  256.             character.
  257.        -m[-]module
  258.  
  259.        -M[-]module
  260.  
  261.        -M[-]'module ...'
  262.  
  263.        -[mM][-]module=arg[,arg]...
  264.             -mmodule executes use module (); before executing
  265.             your script.
  266.  
  267.             -Mmodule executes use module ; before executing your
  268.             script.  You can use quotes to add extra code after
  269.             the module name, e.g., -M'module qw(foo bar)'.
  270.  
  271.             If the first character after the -M or -m is a dash
  272.             (-) then the 'use' is replaced with 'no'.
  273.  
  274.             A little built-in syntactic sugar means you can also
  275.             say -mmodule=foo,bar or -Mmodule=foo,bar as a
  276.             shortcut for -M'module qw(foo bar)'.  This avoids the
  277.             need to use quotes when importing symbols.  The
  278.             actual code generated by -Mmodule=foo,bar is use
  279.             module split(/,/,q{foo,bar}).  Note that the = form
  280.             removes the distinction between -m and -M.
  281.  
  282.        -n   causes Perl to assume the following loop around your
  283.             script, which makes it iterate over filename
  284.             arguments somewhat like sed -n or awk:
  285.  
  286.                 while (<>) {
  287.                     ...             # your script goes here
  288.                 }
  289.  
  290.             Note that the lines are not printed by default.  See
  291.             -p to have lines printed.  Here is an efficient way
  292.             to delete all files older than a week:
  293.  
  294.                 find . -mtime +7 -print | perl -nle 'unlink;'
  295.  
  296.             This is faster than using the -exec switch of find
  297.             because you don't have to start a process on every
  298.             filename found.
  299.  
  300.             BEGIN and END blocks may be used to capture control
  301.             before or after the implicit loop, just as in awk.
  302.  
  303.        -p   causes Perl to assume the following loop around your
  304.             script, which makes it iterate over filename
  305.             arguments somewhat like sed:
  306.  
  307.                 while (<>) {
  308.                     ...             # your script goes here
  309.                 } continue {
  310.                     print;
  311.                 }
  312.  
  313.             Note that the lines are printed automatically.  To
  314.             suppress printing use the -n switch.  A -p overrides
  315.             a -n switch.
  316.  
  317.             BEGIN and END blocks may be used to capture control
  318.             before or after the implicit loop, just as in awk.
  319.  
  320.        -P   causes your script to be run through the C
  321.             preprocessor before compilation by Perl.  (Since both
  322.             comments and cpp directives begin with the #
  323.             character, you should avoid starting comments with
  324.             any words recognized by the C preprocessor such as
  325.             "if", "else" or "define".)
  326.  
  327.        -s   enables some rudimentary switch parsing for switches
  328.             on the command line after the script name but before
  329.             any filename arguments (or before a --).  Any switch
  330.             found there is removed from @ARGV and sets the
  331.             corresponding variable in the Perl script.  The
  332.             following script prints "true" if and only if the
  333.             script is invoked with a -xyz switch.
  334.  
  335.                 #!/usr/bin/perl -s
  336.                 if ($xyz) { print "true\n"; }
  337.  
  338.        -S   makes Perl use the PATH environment variable to
  339.             search for the script (unless the name of the script
  340.             starts with a slash).  Typically this is used to
  341.             emulate #! startup on machines that don't support #!,
  342.             in the following manner:
  343.  
  344.                 #!/usr/bin/perl
  345.                 eval "exec /usr/bin/perl -S $0 $*"
  346.                         if $running_under_some_shell;
  347.  
  348.             The system ignores the first line and feeds the
  349.             script to /bin/sh, which proceeds to try to execute
  350.             the Perl script as a shell script.  The shell
  351.             executes the second line as a normal shell command,
  352.             and thus starts up the Perl interpreter.  On some
  353.             systems $0 doesn't always contain the full pathname,
  354.             so the -S tells Perl to search for the script if
  355.             necessary.  After Perl locates the script, it parses
  356.             the lines and ignores them because the variable
  357.             $running_under_some_shell is never true.  A better
  358.             construct than $* would be ${1+"$@"}, which handles
  359.             embedded spaces and such in the filenames, but
  360.             doesn't work if the script is being interpreted by
  361.             csh.  In order to start up sh rather than csh, some
  362.             systems may have to replace the #! line with a line
  363.             containing just a colon, which will be politely
  364.             ignored by Perl.  Other systems can't control that,
  365.             and need a totally devious construct that will work
  366.             under any of csh, sh or Perl, such as the following:
  367.  
  368.                     eval '(exit $?0)' && eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  369.                     & eval 'exec /usr/bin/perl -S $0 $argv:q'
  370.                             if 0;
  371.  
  372.        -T   forces "taint" checks to be turned on so you can test
  373.             them.  Ordinarily these checks are done only when
  374.             running setuid or setgid.  It's a good idea to turn
  375.             them on explicitly for programs run on another's
  376.             behalf, such as CGI programs.  See the perlsec
  377.             manpage.
  378.  
  379.        -u   causes Perl to dump core after compiling your script.
  380.             You can then take this core dump and turn it into an
  381.             executable file by using the undump program (not
  382.             supplied).  This speeds startup at the expense of
  383.             some disk space (which you can minimize by stripping
  384.             the executable).  (Still, a "hello world" executable
  385.             comes out to about 200K on my machine.)  If you want
  386.             to execute a portion of your script before dumping,
  387.             use the dump() operator instead.  Note: availability
  388.             of undump is platform specific and may not be
  389.             available for a specific port of Perl.
  390.  
  391.        -U   allows Perl to do unsafe operations.  Currently the
  392.             only "unsafe" operations are the unlinking of
  393.             directories while running as superuser, and running
  394.             setuid programs with fatal taint checks turned into
  395.             warnings.
  396.  
  397.        -v   prints the version and patchlevel of your Perl
  398.             executable.
  399.  
  400.        -V   prints summary of the major perl configuration values
  401.             and the current value of @INC.
  402.  
  403.        -V:name
  404.             Prints to STDOUT the value of the named configuration
  405.             variable.
  406.  
  407.        -w   prints warnings about identifiers that are mentioned
  408.             only once, and scalar variables that are used before
  409.             being set.  Also warns about redefined subroutines,
  410.             and references to undefined filehandles or
  411.             filehandles opened readonly that you are attempting
  412.             to write on.  Also warns you if you use values as a
  413.             number that doesn't look like numbers, using an array
  414.             as though it were a scalar, if your subroutines
  415.             recurse more than 100 deep, and innumerable other
  416.             things.  See the perldiag manpage and the perltrap
  417.             manpage.
  418.  
  419.        -x directory
  420.             tells Perl that the script is embedded in a message.
  421.             Leading garbage will be discarded until the first
  422.             line that starts with #! and contains the string
  423.             "perl".  Any meaningful switches on that line will be
  424.             applied (but only one group of switches, as with
  425.             normal #!  processing).  If a directory name is
  426.             specified, Perl will switch to that directory before
  427.             running the script.  The -x switch only controls the
  428.             the disposal of leading garbage.  The script must be
  429.             terminated with __END__ if there is trailing garbage
  430.             to be ignored (the script can process any or all of
  431.             the trailing garbage via the DATA filehandle if
  432.             desired).
  433.